package com.hn.gege.sys.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.gege.sys.modules.sys.entity.QuartzJob;
import com.hn.gege.sys.modules.sys.service.IQuartzJobService;
import com.hn.gege.core.common.CommonPage;
import com.hn.gege.core.common.CommonResult;
import com.hn.gege.core.query.QueryGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.List;


@Slf4j
@Api(tags = "QuartzJobController",description = "定时任务接口")
@Validated
@RestController
@RequestMapping("/sys/quartzJob")
public class QuartzJobController {
	@Autowired
	private IQuartzJobService quartzJobService;

	@ApiOperation("定时任务分页列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
	})
	@GetMapping(value = "/pageInfo")
	public CommonPage<QuartzJob> queryPageList(QuartzJob quartzJob, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
											   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest request) {
		Page<QuartzJob> page = new Page<>(pageNo, pageSize);
		//构造查询条件
		QueryWrapper<QuartzJob> qw = QueryGenerator.initQueryWrapper(quartzJob, request.getParameterMap());
		IPage<QuartzJob> pageList = quartzJobService.page(page, qw);
		return CommonPage.restPage(pageList);
	}

	@ApiOperation("新增定时任务")
	@PostMapping
	public CommonResult add(@Validated QuartzJob quartzJob, BindingResult result) {
		List<QuartzJob> list = quartzJobService.findByJobClassName(quartzJob.getJobClassName());
		if (list!= null && list.size()>0)
			return CommonResult.failed("该定时任务类名已存在");

		boolean b = quartzJobService.saveAndScheduleJob(quartzJob);
		if (b){
		    return CommonResult.success("成功");
		}
		return CommonResult.failed("失败");

	}

	@ApiOperation("修改定时任务")
	@PutMapping
	public CommonResult eidt(@Validated QuartzJob quartzJob, BindingResult result) {
		try {
			quartzJobService.editAndScheduleJob(quartzJob);
		} catch (SchedulerException e) {
			log.error(e.getMessage(),e);
			return CommonResult.failed("该定时任务类名已存在");
		}
		return CommonResult.success("成功");
	}

	@ApiOperation("删除定时任务")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
	@DeleteMapping("/{id}")
	public void delete(@NotBlank@PathVariable("id") String id) {
		QuartzJob quartzJob = quartzJobService.getById(id);
		quartzJobService.deleteAndStopJob(quartzJob);

	}


	@ApiOperation(value = "暂停定时任务")
	@ApiImplicitParam(name = "jobClassName", value = "id", required = true, dataType = "String", paramType = "query")
	@PatchMapping(value = "/pause")
	public CommonResult pauseJob(@NotBlank String id) {
		QuartzJob job = quartzJobService.getById(id);
		quartzJobService.pause(job);
		return CommonResult.success("成功");
	}

	@ApiOperation(value = "恢复定时任务")
	@ApiImplicitParam(name = "jobClassName", value = "id", required = true, dataType = "String", paramType = "query")
	@PatchMapping(value = "/resume")
	public CommonResult resumeJob(@NotBlank String id) {
		QuartzJob job = quartzJobService.getById(id);
		quartzJobService.resumeJob(job);
		return CommonResult.success("成功");
	}

	@ApiOperation("通过id获取任务明细")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
	@GetMapping("/{id}")
	public CommonResult<QuartzJob> queryById(@NotBlank@PathVariable("id") String id) {
		QuartzJob quartzJob = quartzJobService.getById(id);
		return CommonResult.success(quartzJob);
	}


	@ApiOperation("立即执行")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query")
	@PatchMapping("/execute")
	public void execute(@NotBlank String id) {
		QuartzJob quartzJob = quartzJobService.getById(id);
		try {
			quartzJobService.execute(quartzJob);
		} catch (Exception e) {
			//e.printStackTrace();
			log.info("定时任务 立即执行失败>>"+e.getMessage());
		}
	}
}
